package com.java.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.java.dao.ActivityRoomDao;
import com.java.domain.ActivityRoom;
import com.java.domain.event.ActivityRoomEvent;
import com.java.service.RoomService;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class RoomServiceImpl implements RoomService {

	@Resource ActivityRoomDao roomDao;

	@Override
	public TableRespone getList(int page, int size, Integer roomDel) {
		Map<String, Object> params = new HashMap<String, Object>();
		if(!MyTools.isEmpty(roomDel)) {
			params.put("roomDel", roomDel);
		}
		List<ActivityRoomEvent> list = roomDao.selectAll(params, page, size);
		long total = roomDao.selectAllCount(params);
		return new TableRespone(list, total);
	}

	@Override
	public ActivityRoomEvent getOne(String roomId) {
		ActivityRoomEvent info = roomDao.selectByKey(roomId);
		return info;
	}

	@Override
	public BaseRespone saveRoom(ActivityRoom room) {
		int row = 0;
		ActivityRoom info = roomDao.selectByKey(room.getRoomId());
		if(null == info) {
			info = new ActivityRoom()
			.setRoomId(MyTools.getUUID())
			.setRoomTitle(room.getRoomTitle())
			.setRoomNumber(room.getRoomNumber())
			.setRoomAddress(room.getRoomAddress())
			.setRoomDel(0)
			.setRoomTime(new Date());
			row = roomDao.insertSelective(info);
		}else {
			info.setRoomNumber(room.getRoomNumber())
			.setRoomTitle(room.getRoomTitle())
			.setRoomAddress(room.getRoomAddress());
			row = roomDao.updateSelective(info);
		}
		return BaseRespone.result(row, "Save Success");
	}

	@Override
	public BaseRespone delRoom(String roomId) {
		int row = roomDao.deleteUpdate(roomId, "room_del");
		return BaseRespone.result(row, "Delete Success");
	}

	@Override
	public void delRoomForTest(String roomId) {
		roomDao.deleteByKey(roomId);
	}

}
